package com.youku.mson.type;

import java.math.BigDecimal;
import java.math.BigInteger;

import com.youku.mson.util.TypeAdapter;
import com.youku.mson.util.TypeUtils;

public class NumberJsonValue implements JsonValue {

	private String value;

	public NumberJsonValue(String value) {
		this.value = value;
	}

	@Override
	public Object read(TypeAdapter adapter) {
		Class<?> targetClass = adapter.getTargetClass();
		if (TypeUtils.isByte(targetClass)) {
			return Byte.parseByte(value);
		} else if (TypeUtils.isShort(targetClass)) {
			return Short.parseShort(value);
		} else if (TypeUtils.isInteger(targetClass)) {
			return Integer.parseInt(value);
		} else if (TypeUtils.isLong(targetClass)) {
			return Long.parseLong(value);
		} else if (TypeUtils.isFloat(targetClass)) {
			return Float.parseFloat(value);
		} else if (TypeUtils.isDouble(targetClass)) {
			return Double.parseDouble(value);
		} else if (TypeUtils.isBigInteger(targetClass)) {
			return new BigInteger(value);
		} else if (TypeUtils.isBigDecimal(targetClass)) {
			return new BigDecimal(value);
		} else {
			if (value.contains(".")) {
				return Double.parseDouble(value);
			} else {
				return Long.parseLong(value);
			}
		}
	}

}
